package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.SolicitudNuevo;

public class SolicitudNuevoDAOImpl extends JdbcDaoSupport implements ISolicitudNuevoDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM REG_SOLICITUD_NUEVO_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(SolicitudNuevo solicitudNuevo) {
		deleteOperation.execute( solicitudNuevo  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public SolicitudNuevo findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(SolicitudNuevo solicitudNuevo) {
		insertOperation.execute( solicitudNuevo );
	}

	public void update(SolicitudNuevo solicitudNuevo) {
		updateOperation.execute( solicitudNuevo );
	}

	public SolicitudNuevo findByCriteria(SolicitudNuevo  solicitudNuevo) {
		return findByCriteriaQuery.runQuery(solicitudNuevo);
	}

	public List load(SolicitudNuevo  solicitudNuevo) {
		return findAllQuery.runQuery(solicitudNuevo);
	}

	public SolicitudNuevo findByActiveState(SolicitudNuevo  solicitudNuevo) {
		return findByActiveStateQuery.runQuery(solicitudNuevo);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	//Disabled Operations
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO REG_SOLICITUD_NUEVO_T ("
				  + " ID, "
				  + " ID_SOLICITUD, "
				  + " ID_INDICADOR_UBICACION, "
				  + " ID_TIPO_VENTA, "
				  + " ID_CLASE_VENTA, "
				  + " ID_TIPO_AMOBLAMIENTO, "
				  + " HORARIO, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " REG_SOLICITUD_NUEVO_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(SolicitudNuevo solicitudNuevo) {
			InsertOperation.this.update( new Object[]{
				solicitudNuevo.getIdSolicitud(),
				solicitudNuevo.getIdIndicadorUbicacion(),
				solicitudNuevo.getIdTipoVenta(),
				solicitudNuevo.getIdClaseVenta(),
				solicitudNuevo.getIdTipoAmoblamiento(),
				solicitudNuevo.getHorario(),
				solicitudNuevo.getEstado(),
				solicitudNuevo.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE REG_SOLICITUD_NUEVO_T SET "
					  + " ID_SOLICITUD = ? ,"
					  + " ID_INDICADOR_UBICACION = ? ,"
					  + " ID_TIPO_VENTA = ? ,"
					  + " ID_CLASE_VENTA = ? ,"
					  + " ID_TIPO_AMOBLAMIENTO = ? ,"
					  + " HORARIO = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
			  	  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(SolicitudNuevo solicitudNuevo) {
			UpdateOperation.this.update( new Object[]{
				solicitudNuevo.getIdSolicitud(),
				solicitudNuevo.getIdIndicadorUbicacion(),
				solicitudNuevo.getIdTipoVenta(),
				solicitudNuevo.getIdClaseVenta(),
				solicitudNuevo.getIdTipoAmoblamiento(),
				solicitudNuevo.getEstado(),
				solicitudNuevo.getHorario(),
				solicitudNuevo.getUsuarioModifica(),
				solicitudNuevo.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_SOLICITUD_NUEVO_T "
										+ WHERE;
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(SolicitudNuevo solicitudNuevo) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_SOLICITUD_NUEVO_T "
										+ WHERE + " 1=1 "
			  	  	  					+ " ID = ? ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
					new Long(id)
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			SolicitudNuevo solicitudNuevo = new SolicitudNuevo();
				solicitudNuevo.setId(new Long(rs.getLong("ID")));
				solicitudNuevo.setIdSolicitud(new Long(rs.getLong("ID_SOLICITUD")));
				solicitudNuevo.setIdIndicadorUbicacion(rs.getString("ID_INDICADOR_UBICACION"));
				solicitudNuevo.setIdTipoVenta(new Long(rs.getLong("ID_TIPO_VENTA")));
				solicitudNuevo.setIdClaseVenta(new Long(rs.getLong("ID_CLASE_VENTA")));
				solicitudNuevo.setIdTipoAmoblamiento(new Long(rs.getLong("ID_TIPO_AMOBLAMIENTO")));
				solicitudNuevo.setHorario(rs.getString("HORARIO"));
				solicitudNuevo.setEstado(rs.getString("ESTADO"));
				solicitudNuevo.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				solicitudNuevo.setFechaCrea(rs.getDate("FECHA_CREA"));
				solicitudNuevo.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				solicitudNuevo.setFechaModifica(rs.getDate("FECHA_MODIFICA"));
			return solicitudNuevo;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
			  	  	  					+ " ID = ? ";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public SolicitudNuevo runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (SolicitudNuevo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
			  	  	  					+ " AND ID_SOLICITUD = ? "
										+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public SolicitudNuevo runQuery(SolicitudNuevo solicitudNuevo) {
			List result =  execute( new Object[]{
				solicitudNuevo.getIdSolicitud()
				});
			if ( result.size() > 0 ) {
        		return (SolicitudNuevo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
										+ " AND ID_SOLICITUD = ? ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public List runQuery(SolicitudNuevo solicitudNuevo) {
			List result = execute(new Object[] {
										solicitudNuevo.getIdSolicitud()
									});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
			  	  	  					+ " ESTADO = ? ";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public SolicitudNuevo runQuery(SolicitudNuevo solicitudNuevo) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (SolicitudNuevo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
